<?php

declare(strict_types=1);

namespace App\CustHub\Controller\Permission;

use App\CustHub\Annotation\ApiAuth;
use App\CustHub\Annotation\VerifySignature;
use App\CustHub\Controller\BaseController;
use App\CustHub\Request\UserGroupRequest;
use App\CustHub\Service\ChUserGroupService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

#[Controller(prefix: 'ch/permission/group'),ApiAuth,VerifySignature]
class UserGroupController extends BaseController
{
    #[Inject]
    protected ChUserGroupService $service;

    /**
     * 分组列表
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('list')]
    public function list(): \Psr\Http\Message\ResponseInterface
    {
        return $this->success($this->service->list($this->request->all()));
    }

    /**
     * 新增分组
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('save')]
    public function save(UserGroupRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->success(['id' => $this->service->save($request->all())]);
    }

    /**
     * 更新分组
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('update')]
    public function update(UserGroupRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->update($request->input('id'),$request->all()) ? $this->success() : $this->error();
    }

    /**
     * 删除分组
     * @param UserGroupRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('delete')]
    public function delete(UserGroupRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->deleteGroup($request->all()) ? $this->success() : $this->error();
    }

    /**
     * @return \Psr\Http\Message\ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('getInfo')]
    public function getInfo(UserGroupRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->success($this->service->getInfo($request->all()));
    }

    #[PostMapping('getSelectList')]
    public function getSelectList(): \Psr\Http\Message\ResponseInterface
    {
        return $this->success($this->service->getSelectList($this->request->all()));
    }

    #[PostMapping('setSort')]
    public function setSort(UserGroupRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->setSort($request->all()) ? $this->success() : $this->error();
    }
}